package it.truestoryfactory.tsq.game.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import android.content.SharedPreferences;
import android.util.Log;

public class Quiz {
	private String[] topics;
	private int qst4topic;
	private int sec4qst;
	private Integer[][] quizTable;
	
	public static final int MAX_QST_4_TOPIC = 20;
	public static final String[] TOPICS = new String[]{"topic_LOGIC", "topic_MATH"};
	public static final String[] TOPIC_CODES = new String[]{"LOGIC", "MATH"};
	
	private ArrayList<Integer> list;
	
	public Quiz(String[] topics, int qst4topic, int sec4qst) {
		this.topics=topics;
		this.qst4topic=qst4topic;
		this.sec4qst=sec4qst;
	
		quizTable=new Integer[topics.length][qst4topic];
	}
	
	public Quiz(Integer nTopics, Integer qst4topic, Integer sec4qst) {
		this.topics = new String[nTopics];
		this.qst4topic=qst4topic;
		this.sec4qst=sec4qst;
	
		quizTable=new Integer[topics.length][qst4topic];
		
	}

	public Quiz(SharedPreferences sharedPref) {
		int counter = 0;
		for (int i = 0; i < Quiz.TOPICS.length; i++) {
			if (sharedPref.getBoolean(Quiz.TOPICS[i], false)){
				counter++;
			}
		}
		topics = new String[counter];
		counter = 0;
		for (int i = 0; i < Quiz.TOPICS.length; i++) {
			if (sharedPref.getBoolean(Quiz.TOPICS[i], false)){
				topics[counter++] = Quiz.TOPIC_CODES[i]; 
			}
		}
		
		qst4topic = Integer.valueOf(sharedPref.getString("questions", "10"));
		sec4qst = sharedPref.getInt("seconds", 30);
		
		quizTable=new Integer[topics.length][qst4topic];
		
	}

	public void setQuestions() {
		list=new ArrayList<Integer>();
		for (int i = 0; i < MAX_QST_4_TOPIC; i++) {
			list.add(i+1);
		}
		
		
		for (int i = 0; i < quizTable.length; i++) {
			Collections.shuffle(list,new Random(System.currentTimeMillis()));
			for (int j = 0; j < quizTable[i].length; j++) {
				quizTable[i][j] = list.get(j);
			}
		}
	}
	
	public void setQuestions(Integer[][] table) {
		quizTable=table;
	}

	public String[] getTopics() {
		return topics;
	}
	
	public int getQst4topic() {
		return qst4topic;
	}

	public int getSec4qst() {
		return sec4qst;
	}
	
	public Integer[][] getQuizTable() {
		return quizTable;
	}

	public void setTopic(int i, String topicCode) {
		topics[i] = topicCode;
	}

	public void setQuestionNumber(int topicIndex, int questionIndex, Integer questionNumber) {
		quizTable[topicIndex][questionIndex] = questionNumber;
	}

	public void printQuiz() {
		Log.d(this.getClass().getName(), "il quiz ha " + sec4qst + "secondi per ognuna delle " + qst4topic + "per ognuno dei topic");
		Log.d(this.getClass().getName(), "I topic sono:");
		for (int i = 0; i < topics.length; i++) {
			Log.d("topic", (i+1) + ": " + topics[i]);
			for (int j = 0; j < quizTable[i].length; j++) {
				Log.d("question", (j+1) + ": " + quizTable[i][j]);
			}
		}
	}

}